﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SSS.Common;

namespace SSS.Core.Model
{
    public class InventoryType
    {

        private string inventoryID;     //商品id（barcode/isbn）
        public string InventoryID
        {
            get { return inventoryID; }
            set { inventoryID = value; }
        }

        private string inventoryName;    //商品名
        public string InventoryName
        {
            get { return inventoryName; }
            set { inventoryName = value; }
        }

        private string inventoryUnit;   //商品单位
        public string InventoryUnit
        {
            get { return inventoryUnit; }
            set { inventoryUnit = value; }
        }

        private string inventoryLabel;  //商品标签
        public string InventoryLabel
        {
            get { return inventoryLabel; }
            set { inventoryLabel = value; }
        }

        private string inventoryPicurl; //图片地址
        public string InventoryPicurl
        {
            get { return inventoryPicurl; }
            set { inventoryPicurl = value; }
        }

        private string format = "{0}\"inventoryID\":{1},\"inventoryName\":{2},\"inventoryUnit\":{3},\"inventoryLabel\":{4},\"inventoryPicurl\":{5}{6}";
        public InventoryType(string inventoryID, string inventoryName, string inventoryUnit, string inventoryLabel, string inventoryPicurl)
        {
            this.inventoryID = inventoryID;
            this.inventoryName = inventoryName;
            this.inventoryUnit = inventoryUnit;
            this.inventoryLabel = inventoryLabel;
            this.inventoryPicurl = inventoryPicurl;
        }

        public override string ToString()
        {
            return String.Format( format,
                "{",
                inventoryID == null ? "null" : String.Concat("\"",inventoryID,"\""),
                inventoryName == null ? "null" : String.Concat( "\"", inventoryName, "\"" ),
                inventoryUnit == null ? "null" : String.Concat( "\"", inventoryUnit, "\"" ),
                inventoryLabel == null ? "null" : String.Concat( "\"", inventoryLabel, "\"" ),
                inventoryPicurl == null ? "null" : String.Concat( "\"", inventoryPicurl, "\"" ),
                "}"
                );
        }

        public InventoryType GetInventoryTypeByJson( string Json )
        {
            char[] trims = { '{', '}', '[', ']', '"', '\\', ' '};
            string[] strs = Json.Split( ',' );
            List<KeyPair> l = new List<KeyPair>();
            foreach ( string s in strs )
            {
                string[] sb = s.Split( ':' );
                KeyPair k = new KeyPair( sb[ 0 ], sb[ 1 ] );
                l.Add( k );
            }
            for ( int i = 0; i < l.Count; i++ )
            {
                l[ i ].Key = l[ i ].Key.TrimStart( trims );
                l[ i ].Key = l[ i ].Key.TrimEnd( trims );
            }
            string inventoryID = "";
            string inventoryName = "";
            string inventoryUnit = ""; ;
            string inventoryLabel = ""; ;
            string inventoryPicurl = ""; ;
            for (int i = 0; i < l.Count; i++)
            {
                switch(l[i].Key)
                {
                    case "inventoryID":
                        inventoryID = l[i].Value;
                        break;
                    case "inventoryName":
                        inventoryName = l[ i ].Value;
                        break;
                    case "inventoryUnit":
                        inventoryUnit = l[ i ].Value;
                        break;
                    case "inventoryLabel":
                        inventoryLabel = l[ i ].Value;
                        break;
                    case "inventoryPicurl":
                        inventoryPicurl = l[ i ].Value;
                        break;
                }
            }
            return new InventoryType( inventoryID, inventoryName, inventoryUnit, inventoryLabel, inventoryPicurl );

            
        }

    }
}
